#include "threadPool.h"
#include "keyword.h"

int makeWorker(threadPool_t *pthreadPool){
    //创建若干个子线程
    for(int i = 0; i < pthreadPool->threadNum; ++i){
        pthread_create(&pthreadPool->tidArr[i],NULL,threadFunc,pthreadPool);
    }
    return 0;
}
//void *threadFunc(void *arg){
//    threadPool_t *pthreadPool = (threadPool_t *)arg;
//    while(1){
//        pthread_mutex_lock(&pthreadPool->taskQueue.mutex);
//        while(pthreadPool->taskQueue.queueSize == 0){
//            pthread_cond_wait(&pthreadPool->taskQueue.cond,&pthreadPool->taskQueue.mutex);
//        }
//        int netfd = pthreadPool->taskQueue.pFront->netfd;
//        taskDequeue(&pthreadPool->taskQueue);
//        pthread_mutex_unlock(&pthreadPool->taskQueue.mutex);
//        transFile(netfd);
//        close(netfd);
//        printf("child thread finished task!\n");
//    }
//}
void cleanupMutex(void *arg){
    threadPool_t *pthreadPool = (threadPool_t *)arg;
    printf("unlock\n");
    pthread_mutex_unlock(&pthreadPool->taskQueue.mutex);
}
void *threadFunc(void *arg){
    threadPool_t *pthreadPool = (threadPool_t *)arg;
    while(1){
        char buf[1024];
        fd_set rdset;
        int netfd;
        pthread_mutex_lock(&pthreadPool->taskQueue.mutex);
        //pthread_cleanup_push(cleanupMutex,pthreadPool);
        if(pthreadPool->exitflag == 1){
            printf("I am going to die!\n");
            pthread_mutex_unlock(&pthreadPool->taskQueue.mutex);
            pthread_exit(NULL);
        }
        while(pthreadPool->taskQueue.queueSize == 0){
            if(pthreadPool->exitflag == 1){
                printf("I am going to die!\n");
                pthread_mutex_unlock(&pthreadPool->taskQueue.mutex);
                pthread_exit(NULL);
            }
            pthread_cond_wait(&pthreadPool->taskQueue.cond,&pthreadPool->taskQueue.mutex);
        }

        netfd = pthreadPool->taskQueue.pFront->netfd;

        while(1)
        {
            FD_ZERO(&rdset);
            FD_SET(netfd,&rdset);
            if(FD_ISSET(netfd,&rdset))
            {
                memset(buf,0,sizeof(buf));
                ssize_t sret = recv(netfd,buf,sizeof(buf),0);
                if(sret == 0)
                {
                    printf("一个客户端关闭关闭！\n");
                    goto end;
                }

                //接收命令和文件名/路径
                //char cmd[16]={0};
                char std[1024]={0};
                for(unsigned long i=0;i<strlen(buf)-1;i++)
                {
                    std[i]=buf[i];
                }
                char cmd[16]={0};
                char filename[128]={0};
                int retone = sprintf(cmd,"%s",strtok(std," "));
                int rettwo = sprintf(filename,"%s",strtok(NULL," "));
                printf("%s,retone = %d\n",cmd,retone);
                printf("%s,rettwo = %d\n",filename,rettwo);


                if(strcmp("cd\0",cmd) == 0)
                {
                    //do_cd();
                    char *rootpath = getcwd(NULL,0);
                    DIR *dir = opendir(rootpath);
                    // printf("DIR!%stail\n",rootpath);
                    send(netfd,rootpath,20,0);
                    struct dirent *pdir;
                    //printf("pdir!\n");
                    while((pdir = readdir(dir)) != NULL)
                    {
                        //printf("%s\n",pdir->d_name);
                        //send(netfd,"filename\n",20,0);
                        if(strcmp(pdir->d_name,filename) == 0)
                        {
                            //char *rootpath = getcwd()
                            char dir_name[1024]={0};
                            sprintf(dir_name,"%s/%s",getcwd(NULL,0),filename);
                            chdir(dir_name);
                            printf("%s\n",dir_name);
                            
                            char sendcontent[1024]={0};
                            sprintf(sendcontent,"%s进入文件成功！\n",dir_name);
                            send(netfd,sendcontent,strlen(sendcontent),0);
                        }
                    }

                }
                else if(strcmp("ls\0",cmd) == 0)
                {

                    char * rootpath = getcwd(NULL,0);
                    DIR * dir = opendir(rootpath);
                    struct dirent * pdir;
                    char send_name[1024]={0};
                    while((pdir=readdir(dir))!=NULL)
                    {
                        char ls_name[1024]={0};
                        sprintf(ls_name,"%s\n",pdir->d_name);
                        strcat(send_name,ls_name);
                    }
                    
                    send(netfd,send_name,strlen(send_name),0);

                }
                else if(strcmp("mkdir\0",cmd) == 0)
                {

                    char *rootpath = getcwd(NULL,0);
                    char dir_name[1024]={0};
                    sprintf(dir_name,"%s/%s",rootpath,filename);
                    mkdir(dir_name,0777);
                    //sprintf(dir_name,"%s tail!",dir_name);
                    send(netfd,dir_name,strlen(dir_name),0);
                    send(netfd,"目录创建成功\n",20,0);

                }
                else if(strcmp("remove\0",cmd) == 0)
                {
                    char *rootpath = getcwd(NULL,0);
                    DIR *dir = opendir(rootpath);
                    //printf("DIR!%stail\n",rootpath);
                    struct dirent *pdir;
                    //printf("pdir!\n");
                    while((pdir = readdir(dir)) != NULL)
                    {
                        //printf("%s\n",pdir->d_name);
                        //send(netfd,"filename\n",20,0);
                        if(strcmp(pdir->d_name,filename) == 0)
                        {
                            rmdir(filename);
                            send(netfd,"删除文件成功！\n",20,0);
                            break;
                        }
                    }

                }
                else if(strcmp("gets\0",cmd) == 0)
                {
                    send(netfd,"1",1,0);
                    char *rootpath = getcwd(NULL,0);
                    DIR *dir = opendir(rootpath);
                    struct dirent * pdir;
                    while((pdir = readdir(dir)) != NULL)
                    {
                        if(strcmp(pdir->d_name,filename) == 0)
                        {
                            transFile(netfd,filename);
                            break;
                        }
                    }
                }
                else if(strcmp("puts\0",cmd) == 0)
                {
                    char idx[10];
                    recv(netfd,idx,sizeof(idx),0);
                    printf("%s,strlen = %ld\n",idx,strlen(idx));
                    if(strcmp("1\0",idx) == 0)
                    {
                    printf("准备接收文件！\n");
                    //recvFile(netfd);
                    }
                }
                else if(strcmp("exit\0",cmd) == 0)
                {

                    goto end;
                }
                else
                {
                    send(netfd,"非法命令，请重新输入\n",30,0);
                }
            }
        }
end:
        taskDequeue(&pthreadPool->taskQueue);
        pthread_mutex_unlock(&pthreadPool->taskQueue.mutex);
        //pthread_cleanup_pop(1);

        //memset(buf,0,sizeof(buf));
        //recv(netfd,buf,strlen(buf),0);
        //printf("worker : %s\n",buf);


        close(netfd);
        printf("child thread finished task!\n");
    }
}
